package com.stylefeng.guns.modular.system.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.model.CommonKeyValue;
import com.stylefeng.guns.core.model.Pager;
import com.stylefeng.guns.modular.system.dao.SysPrivilegeMapper;
import com.stylefeng.guns.modular.system.model.SysPrivilege;
import com.stylefeng.guns.modular.system.service.ISysPrivilegeService;
import org.aspectj.lang.annotation.Around;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**

 * 系统授权表 Service 实现 
 * @author sasazaixian
 * @Date 2018-10-09 13:30
 */
@Service
public class SysPrivilegeServiceImpl extends ServiceImpl<SysPrivilegeMapper,SysPrivilege> implements ISysPrivilegeService {

    @Inject
    @Named("sysPrivilegeMapper")
    private SysPrivilegeMapper sysPrivilegeMapper;

    /**
     * 删除系统授权表
     * @param sysPrivilege
     * @return     
     */
    public int deleteSysPrivilege(SysPrivilege sysPrivilege)throws DataAccessException {
        if(sysPrivilege==null ||  sysPrivilege.getSpType()==null ){
            throw new RuntimeException("参数不全！");
        }

        return this.sysPrivilegeMapper.deleteSysPrivilege(sysPrivilege);
    }
    /**
     * 批量删除系统授权表
     * @param ids 主键ID数组
     * @return 
     */
    public int deleteBatchSysPrivilege(String[] ids)throws DataAccessException{
        return this.sysPrivilegeMapper.deleteBatchSysPrivilege(ids);
    }
    /**
     * 查询系统授权表列表
     * @param sysPrivilege 系统授权表对象
     * @return
     */
    public List<SysPrivilege> selectListSysPrivilege(SysPrivilege sysPrivilege)throws DataAccessException{
        return  this.sysPrivilegeMapper.selectListSysPrivilege(sysPrivilege);
    }
    /**
	 * 分页查询系统授权表列表
     * @param pager 分页对象
     * @return
     */     
    public  Map pageListSysPrivilege(Pager pager) throws DataAccessException{
        Page page=new Page(pager.getPageNumber(),pager.getPageSize());
        page.setRecords(sysPrivilegeMapper.selectListSysPrivilege(page,(SysPrivilege)pager.getObj()));
        List<SysPrivilege> list = page.getRecords();
        Map mapDate = new HashMap();
        mapDate.put("rows", list);
        mapDate.put("total", page.getTotal());
        mapDate.put("page", page.getCurrent());
        mapDate.put("pageCount", page.getPages());
        return mapDate;
    }
    /**
     * 同一个事务 互斥锁查询
     * @param sysPrivilege 系统授权表对象
     * @return 
     */
    public  List<SysPrivilege> selectListSysPrivilegeForUpdate(SysPrivilege sysPrivilege)throws DataAccessException{
        return  this.sysPrivilegeMapper.selectListSysPrivilegeForUpdate(sysPrivilege);
    }
    /**
     * 通过主键id查询的系统授权表列表
     * @param id 主键id
     * @return     
     */
    public  SysPrivilege findSysPrivilegeById(String id)throws DataAccessException{
        return  this.sysPrivilegeMapper.findSysPrivilegeById(id);
    }

    /**
     * 新增系统授权表数据
     * @param sysPrivilege  系统授权表对象
     * @return
     */
    public int insertSysPrivilege(SysPrivilege sysPrivilege) throws DataAccessException {
        /*********如果主键是UUID 可以启用以下方式 仅供参考*************/
        /*if(sysPrivilege!=null && (sysPrivilege.getId()==null || sysPrivilege.getId().trim().length()<10)){
            sysPrivilege.setId(IdWorker.get32UUID());
        }*/
        //sysPrivilege.setCreateTime(DateTime.now());
        //String subject = JwtTokenUtil.getClaimFromToken(token).getSubject();//用户ID
        //if (StringUtils.isNotEmpty(subject)) {
        //     User user = this.userService.selectById(subject);
        //     sysPrivilege.setCreateUser(""+user.getId());
        //}
        if(sysPrivilege.getSoId()==null){
            //根据当前用户获取当前根机构id
            sysPrivilege.setSoId(-1);
        }
        this.sysPrivilegeMapper.insertSysPrivilege(sysPrivilege);
        return sysPrivilege.getSpId();//返回主键
    }
    /**
     * 更新系统授权表数据
     * @param sysPrivilege 系统授权表对象
     * @return     
     */
    public int updateSysPrivilege(SysPrivilege sysPrivilege) throws DataAccessException {
     	//sysPrivilege.setUpdateTime(DateTime.now());
        //String subject = JwtTokenUtil.getClaimFromToken(token).getSubject();//用户ID
        //if (StringUtils.isNotEmpty(subject)) {
        //    User user = this.userService.selectById(subject);
        //    orgRole.setUpdateUser(""+user.getId());
        //}
        return  this.sysPrivilegeMapper.updateSysPrivilege(sysPrivilege);
    }    
    /**
     * 查询系统授权表返回key-Value属性
     * @param sysPrivilege 系统操作对象
     * @return     
     */
    public List<CommonKeyValue> optionsSysPrivilege(SysPrivilege sysPrivilege) throws DataAccessException {
        List<SysPrivilege> listSysPrivilege=this.sysPrivilegeMapper.selectListSysPrivilege(sysPrivilege);
        List<CommonKeyValue> listOptions=new ArrayList<CommonKeyValue>();
        for(SysPrivilege item:listSysPrivilege){
            CommonKeyValue opts=new CommonKeyValue();
            opts.setId(""+item.getSpId());
            opts.setValue(""+item.getSpId());
            listOptions.add(opts);
        }
        return listOptions;
    }
}
